<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_students extends CI_Model {

    private $tmp = '';

    function __construct() {
        parent::__construct();
    }

    public function add_visit($subject) {
        $ids = $this->input->post('ids');
        $ids = explode(':', $ids);
        $data['gst_id'] = $subject;
        $data['student_id'] = $ids[0];
        $data['subject_day_id'] = $ids[1];
        $data['value'] = $this->input->post('visit');
        $data['score'] = $data['value'] == 'true' ? intval($this->input->post('score')) : 0;
        $data['score'] = ($data['score'] < 0 OR $data['score'] > 100) ? 0 : $data['score'];
        $data['date'] = date('Y-m-d h:i:m');
        $this->db->insert('visits', $data);
        return $this->db->insert_id();
    }

    public function edit_visit() {
        $ids = $this->input->post('edit_ids');
        $ids = explode(':', $ids);
        //$data['gst_id'] = $subject;
        $student_id = $ids[0];
        $subject_day_id = $ids[1];
        $data['value'] = $this->input->post('edit_visit');
        $data['score'] = $data['value'] == 'true' ? intval($this->input->post('edit_score')) : 0;
        $data['score'] = ($data['score'] < 0 OR $data['score'] > 100) ? 0 : $data['score'];
        $data['date'] = date('Y-m-d h:i:m');
        $this->db->where('subject_day_id', $subject_day_id);
        $this->db->where('student_id', $student_id);
        $this->db->update('visits', $data);
        return $this->db->_error_number();
        //return $this->db->insert_id();
    }

    public function get_students() {
        $query = $this->db->get('students');
        return $query->result_array();
    }

    public function get_one_student($id = 0) {
        $query = $this->db->query('SELECT * FROM students WHERE id = "' . $id . '" ');
        return $query->result_array();
    }

    public function get_group_student($id = 0) {
        $query = $this->db->query('SELECT id_group FROM students WHERE id = "' . $id . '" ');
        $query->result_array();
        $row = $query->row();
        return $row->id_group;
    }

    public function insert_students($students = array(), $id_group = '') {

        $students = preg_split("/[,]/", $students);
        $students = array_diff($students, array(''));
        $students = array_map('trim', $students);
        $students = array_unique($students);

        foreach ($students as $names) {
            $query = $this->db->query('SELECT id,name FROM students WHERE name = "' . $names . '" AND id_group = "' . $id_group . '" ');
            if ($query->num_rows() > 0) {
                $this->tmp .= $names . ',';
            }
        }

        if ($this->tmp == '') {
            foreach ($students as $names2) {
                $this->db->insert('students', array('name' => $names2, 'id_group' => $id_group));
            }
            return lang('successfull');
        } else {
            return sprintf(lang('admin_name_required'), $this->tmp);
        }
    }

    public function edit_student($id = 0, $name = '', $login = '', $password = '', $email = '', $id_group = '') {
        $query = $this->db->query('SELECT id,login FROM students WHERE login = "' . $login . '" ');
        if ($query->num_rows() > 0) {
            $row = $query->row();
            if ($row->id != $id) {
                return sprintf(lang('admin_login_required'), $login);
            } else {
                $this->db->update('students', array(
                    'name' => $name,
                    'login' => $login,
                    'password' => $password,
                    'email' => $email,
                    'id_group' => $id_group), array('id' => $id));
                return lang('successfull');
            }
        } else {
            $this->db->update('students', array(
                'name' => $name,
                'login' => $login,
                'password' => $password,
                'email' => $email,
                'id_group' => $id_group), array('id' => $id));
            return lang('successfull');
        }
    }

    public function delete_student($students = array()) {

        foreach ($students as $id) {
            $this->db->delete('students', array('id' => $id));
        }
    }

}

/* End of file m_teachers.php */
/* Location: ./application/models/m_teachers.php */